package com.codedancer.campus.po;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.codedancer.campus.common.typehandler.CodeEnumTypeHandler;
import com.codedancer.campus.enums.DynamicStatus;
import com.codedancer.campus.enums.DynamicType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@TableName(value = "dynamic", autoResultMap = true)
@Table(name = "dynamic", comment = "动态")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Dynamic extends Model<Dynamic> implements Serializable {

    @TableId
    @TableField("uuid")
    @Column(name = "uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isKey = true, isNull = false, comment = "uuid")
    private String uuid;

    @TableField("content")
    @Column(name = "content", type = MySqlTypeConstant.TEXT, isNull = false, comment = "内容")
    private String content;

    @TableField("publish_user_uuid")
    @Column(name = "publish_user_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "发布人uuid")
    private String publishUserUuid;

    @TableField("publish_school_uuid")
    @Column(name = "publish_school_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "发布学校uuid")
    private String publishSchoolUuid;

    @TableField("plate_uuid")
    @Column(name = "plate_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "板块uuid")
    private String plateUuid;

    @TableField("circle_uuid")
    @Column(name = "circle_uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isNull = false, comment = "发布圈子")
    private String circleUuid;

    @TableField("deleted")
    @Column(name = "deleted", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "是否删除 1表示删除 0表示未删除", defaultValue = "0")
    private Short deleted;

    @TableField("examine_count")
    @Column(name = "examine_count", type = MySqlTypeConstant.INT, length = 1, isNull = false, comment = "审核次数，最多审核五次", defaultValue = "0")
    private Integer examineCount;

    @TableField("view_count")
    @Column(name = "view_count", type = MySqlTypeConstant.INT, length = 10, isNull = false, comment = "浏览量", defaultValue = "0")
    private Integer viewCount;

    @TableField("comment_count")
    @Column(name = "comment_count", type = MySqlTypeConstant.INT, length = 10, isNull = false, comment = "评论数量", defaultValue = "0")
    private Integer commentCount;

    @TableField("kudos_count")
    @Column(name = "kudos_count", type = MySqlTypeConstant.INT, length = 10, isNull = false, comment = "点赞数量", defaultValue = "0")
    private Integer kudosCount;

    @TableField("is_open_comment")
    @Column(name = "is_open_comment", type = MySqlTypeConstant.BIT, isNull = false, comment = "是否开启评论", defaultValue = "true")
    private Boolean isOpenComment;

    @TableField("is_hide")
    @Column(name = "is_hide", type = MySqlTypeConstant.BIT, isNull = false, comment = "是否隐藏", defaultValue = "false")
    private Boolean isHide;

    @TableField(value = "type", typeHandler = CodeEnumTypeHandler.class)
    @Column(name = "type", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "动态类型 1表示悄悄话 2表示提问")
    private DynamicType type;

    @TableField(value = "status", typeHandler = CodeEnumTypeHandler.class)
    @Column(name = "status", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "动态状态 1表示待审核 2表示未通过 3表示已通过")
    private DynamicStatus status;

    @Column(name = "create_time", type = MySqlTypeConstant.DATETIME, isNull = false, comment = "创建时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Column(name = "update_time", type = MySqlTypeConstant.DATETIME, isNull = false, comment = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @TableField("is_sync_liberty_square")
    @Column(name = "is_sync_liberty_square", type = MySqlTypeConstant.BIT, length = 1, isNull = false, comment = "是否同步到自由广场", defaultValue = "false")
    private Boolean isSyncLibertySquare;

    // 发布人
    @TableField(exist = false)
    private User publishUser;

    // 发布学校
    @TableField(exist = false)
    private School publishSchool;

    // 发布圈子
    @TableField(exist = false)
    private Circle publishCircle;

    // 图片
    @TableField(exist = false)
    private List<DynamicImage> dynamicImages;

    // 动态圈子标签
    @TableField(exist = false)
    private List<CircleLabel> circleLabels;

    /**
     * 审核次数是否大于五次
     *
     * @return
     */
    public boolean examineCountIsGtFive() {
        return examineCount >= 5;
    }

    /**
     * 是否通过审核
     *
     * @return
     */
    public boolean isPassExamine() {
        return status.equals(DynamicStatus.PASSED);
    }

    /**
     * 动态是否可以点赞或者取消点赞 有以下情况：1.处于待审核、未通过
     *
     * @return
     */
    public boolean isCanKudosOrCancel() {
        if (!status.equals(DynamicStatus.PASSED)) {
            return false;
        }
        return true;
    }
}
